<template>
  <el-dialog
    v-model="state.dialogFormVisible"
    :title="t('menu.menuInfo')"
    custom-class="dialog-min-width"
    width="400px"
    :destroy-on-close="true"
  >
    <el-form ref="formRef" :model="formEdit" :rules="rules" label-width="120px">
      <el-row>
        <el-col :lg="24" :md="24" :sm="24" :xs="24">
          <el-form-item :label="t('menu.menuName')" prop="menuName">
            <el-input v-model="formEdit.menuName" maxlength="64" clearable>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="24" :md="24" :sm="24" :xs="24">
          <el-form-item :label="t('menu.menuCode')" prop="menuCode">
            <el-input v-model="formEdit.menuCode" maxlength="64" clearable>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="24" :md="24" :sm="24" :xs="24">
          <el-form-item :label="t('menu.description')">
            <el-input
              v-model="formEdit.description"
              maxlength="64"
              type="textarea"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="state.dialogFormVisible = false">{{
          t("common.cancelButton")
        }}</el-button>
        <el-button type="primary" @click="handleSubmit">{{
          t("common.submitButton")
        }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import utils from "../../../utils/utils";
import apiMenu from "../api/menu";
import { useI18n } from "vue-i18n";
const { t } = useI18n({ useScope: "global" });
const formRef = ref();

const formEditOriginal = () => {
  return {
    menuId: "",
    menuCode: "",
    menuName: "",
    description: "",
  };
};
const formEdit = reactive(formEditOriginal());

const rules = reactive({
  menuName: [
    {
      required: true,
      message: () => t("common.required"),
      trigger: "blur",
    },
  ],
  menuCode: [
    {
      required: true,
      message: () => t("common.required"),
      trigger: "blur",
    },
  ],
});

const state = reactive({
  dialogFormVisible: false,
});

const emit = defineEmits<{
  (event: "on-submit-success", row: any): void;
}>();

const handleSubmit = async () => {
  await formRef.value.validate();

  apiMenu
    .updateMenu(formEdit)
    .then((response: any) => {
      if (response.data.code == 200) {
        state.dialogFormVisible = false;
        utils.infoNotification(t("common.submitSuccess"));
        emit("on-submit-success", formEdit);
      }
    })
    .catch((err: any) => {
      utils.errorNotification(t("common.submitException"), err);
    });
};

const show = (row: any) => {
  // console.log(row);
  Object.assign(formEdit, formEditOriginal());
  if (row != undefined) {
    apiMenu
      .getMenu(row.menuId)
      .then((response: any) => {
        if (response.data.code == 200) {
          Object.assign(formEdit, response.data.data);
        }
      })
      .catch((err: any) => {
        utils.errorNotification(t("common.submitException"), err);
      });
  }
  // 触发刷新树
  state.dialogFormVisible = true;
  // 重置表单校验
  if (formRef.value) {
    formRef.value.resetFields();
  }
};

defineExpose({
  show,
});
</script>
